package com.cn.web;

import com.cn.entity.GenProperty;
import com.cn.entity.Generate;
import com.cn.service.GenPropertyService;
import com.cn.service.GenerateService;
import com.cn.utlis.FreeMarkerUtils;
import com.cn.vo.BaseVo;
import com.cn.vo.GenCodeVo;
import com.cn.vo.GenPropertyVo;
import com.cn.vo.ReJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by chenyuncong on 2018/9/17.
 */
@RestController
@RequestMapping("gen")
public class GenerateController {

    @Value("${local.path}")
    private String localPath;

    @Value("${local.package}")
    private String localPackage;

    @Autowired
    private GenerateService generateService;

    @Autowired
    private GenPropertyService genPropertyService;

    @Autowired
    private FreeMarkerUtils freeMarkerUtils;

    /**
     * 获取列表
     * @return
     */
    @RequestMapping("getList")
    public ReJson getList(BaseVo vo) throws Exception {
        ReJson rejson=new ReJson(0,"");
        String sql ="select "+generateService.columnStr()+" from generate where 1=1";
        List<Generate> list = generateService.getList(sql,Generate.class);
        rejson.set(list.size(),list);
        return rejson;
    }

    @RequestMapping("getProList")
    public ReJson getProList(BaseVo vo,long generateId) throws Exception {
        ReJson rejson=new ReJson(0,"");
        String sql ="select "+genPropertyService.columnStr()+" from gen_property where 1=1 and generate_Id=?";
        List<GenProperty> list = genPropertyService.getList(sql,GenProperty.class,generateId);
        rejson.set(list.size(),list);
        return rejson;
    }
    /**
     * 保存数据
     * @return
     */
    public String save(){
        return null;
    }

    /**
     * 保存属性
     * @return
     */
    @PostMapping("proSave")
    public String proSave(GenPropertyVo vo){
        if(StringUtils.isEmpty(vo.getGenerateId())){
            return "1";
        }
        try {
            genPropertyService.save(new GenProperty(vo.getGenerateId()));
        } catch (Exception e) {
            e.printStackTrace();
            return "5";
        }
        return "0";
    }
    /**
     * 更新数据
     * @return
     */
    public String update(){
        return null;
    }

    @PostMapping("proUpdate")
    public String proUpdate(GenPropertyVo vo){
        if(StringUtils.isEmpty(vo.getId())){
            return "1";
        }
        if(StringUtils.isEmpty(vo.getGenerateId())){
            return "2";
        }
        if(StringUtils.isEmpty(vo.getPropertyName())){
            return "3";
        }
        if(StringUtils.isEmpty(vo.getPropertyType())){
            return "4";
        }
/*        if(StringUtils.isEmpty(vo.getWebPropertyName())){
            return "5";
        }
        if(StringUtils.isEmpty(vo.getWebPropertyLength())){
            return "6";
        }*/

        try {
            genPropertyService.updateGP(vo);
        } catch (Exception e) {
            e.printStackTrace();
            return "-1";
        }
        return "0";
    }
    /**
     * 删除数据
     * @return
     */
    public String delete(){
        return null;
    }

    /**
     * 生成代码
     * @param genId
     * @return
     */
    @RequestMapping("genCode")
    public String genCode(Long genId,String localPath) throws Exception {
        Map<String,Object> root = new HashMap<>();
        Generate gen = generateService.get(genId);

//        String entityName, String spaceEntityName, String headSmallEntityName, List<GenPropertyVo> propertyVoList

        List<GenPropertyVo> gpList = genPropertyService.getGenPropertyVoList(genId);
        StringBuffer paramStr=new StringBuffer();
        StringBuffer voParams=new StringBuffer();
        gpList.forEach(x->{
            if(!x.getPropertyName().equals("id")){
                paramStr.append(x.getPropertyType()).append(" ").append(x.getPropertyName()).append(",");
                voParams.append("vo.get").append(x.getHeadBigPropertyName()).append("(),");
            }
        });
        if(paramStr.length()>0){
            paramStr.deleteCharAt(paramStr.length()-1);
            voParams.deleteCharAt(voParams.length()-1);
        }
        GenCodeVo gvo = new GenCodeVo(gen.getEntityName(),FreeMarkerUtils.upperCharToUnderLine(gen.getEntityName()),FreeMarkerUtils.smallName(gen.getEntityName()),gpList,paramStr.toString(),gen.getNotes(),voParams.toString());
        root.put("gvo",gvo);
        root.put("localPackage",localPackage);

        freeMarkerUtils.createFile("\\free\\entity.ftl",gvo.getEntityName()+".java",root);
        freeMarkerUtils.createFile("\\free\\vo.ftl",gvo.getEntityName()+"VO.java",root);
        freeMarkerUtils.createFile("\\free\\dao.ftl",gvo.getEntityName()+"Dao.java",root);
        freeMarkerUtils.createFile("\\free\\daoImpl.ftl",gvo.getEntityName()+"DaoImpl.java",root);

        freeMarkerUtils.createFile("\\free\\service.ftl",gvo.getEntityName()+"Service.java",root);
        freeMarkerUtils.createFile("\\free\\serviceImpl.ftl",gvo.getEntityName()+"ServiceImpl.java",root);

        freeMarkerUtils.createFile("\\free\\controller.ftl",gvo.getEntityName()+"Controller.java",root);

        freeMarkerUtils.createFile("\\free\\jsp.ftl",gvo.getSpaceEntityName()+".jsp",root);
        freeMarkerUtils.createFile("\\free\\js.ftl",gvo.getSpaceEntityName()+".js",root);
        return "1";
    }
}

